/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing;

import cz.muni.fi.pv168.uloha3.entities.Owner;
import cz.muni.fi.pv168.uloha3.entities.RealEstate;
import cz.muni.fi.pv168.uloha3.swing.workers.EstateListAllSwingWorker;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author morty
 */
public class EstatesTableModel extends AbstractTableModel{

    private List<RealEstate> estatesList = new ArrayList<RealEstate>();
    private Locale localization = Locale.getDefault();
    
    public EstatesTableModel() {
        EstateListAllSwingWorker worker = new EstateListAllSwingWorker(this);
        worker.execute();
    }
    
    public EstatesTableModel(Owner excludeForOwner) {
        EstateListAllSwingWorker worker = new EstateListAllSwingWorker(excludeForOwner, this);
        worker.execute();
    }
    
    @Override
    public int getRowCount() {
        return estatesList.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        RealEstate estate = estatesList.get(rowIndex);
        switch(columnIndex) {
            case 0:
                return estate.getCatastralId();
            case 1:
                return estate.getAddress();
            case 2:
                return estate.getType().getValue();
            case 3:
                return estate.getId().toString();
            default:
                throw new IllegalArgumentException(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/EstatesTableModel", localization).getString("COLUMNINDEX"));
        }
    }
    
    @Override
    public String getColumnName(int columnIndex) {
        switch(columnIndex) {
            case 0:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/EstatesTableModel", localization).getString("CATASTRAL ID");
            case 1:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/EstatesTableModel", localization).getString("ADDRESS");
            case 2:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/EstatesTableModel", localization).getString("TYPE");
            case 3:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/EstatesTableModel", localization).getString("ID");
            default:
                throw new IllegalStateException(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/EstatesTableModel", localization).getString("COLUMNINDEX"));
        }
    }

    public void clearAllEstates() {
        this.estatesList.clear();
        fireTableDataChanged();
    }

    public void addAllEstates(Set<RealEstate> estates) {
        this.estatesList.addAll(estates);
        fireTableDataChanged();
    }
    
    public void addEstate(RealEstate estate) {
        estatesList.add(estate);
        int lastRow = estatesList.size()-1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void removeEstate(RealEstate estate) {
        int rownum = estatesList.indexOf(estate);
        estatesList.remove(rownum);
        fireTableRowsDeleted(rownum, rownum);
    }
    
    public void updateEstate(RealEstate estate) {
        estatesList.set(estatesList.indexOf(estate), estate);
        fireTableRowsUpdated(estatesList.indexOf(estate), estatesList.indexOf(estate));
    }
    
    public RealEstate getEstateAt(int rownum) {
        return estatesList.get(rownum);
    }
}
